package com.third.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.third.entity.Brand;
import com.third.entity.R;
import com.third.entity.RegisterApp;
import com.third.entity.RestBean;
import com.third.entity.Users;
import com.third.mapper.UsersMapper;
import com.third.service.UsersService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 谢添发
 * @version 1.1
 * @date 2023/10/24 23:47
 */
@RestController
@RequestMapping("/user")
public class User_Controller {

    @Resource
    private UsersService userService;
    @Resource
    UsersMapper usersMapper;

    @PostMapping("/save")
    public void saveUser(Users user){
        userService.save(user);
    }

    @DeleteMapping("/delete")
    public void deleteUserById(int id){
        userService.removeById(id);
    }

    @PutMapping("/update")
    public void updateUser(Users user){
        userService.updateById(user);
    }

    @GetMapping("/page")
    public R getPage(@PathVariable int currentPage, @PathVariable int size , Users user){
        Page<Users> page = new Page<>(currentPage, size);
        QueryWrapper<Users> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",user.getUserId()).eq("username",user.getUsername())
                .eq("password",user.getPassword()).eq("role_id",user.getRoleId())
                .eq("phone",user.getPhone()).eq("warehouse",user.getAddress())
                .eq("email",user.getEmail());
        Page<Users> Ipage = userService.page(page, wrapper);
        return new R(true,Ipage.getRecords());
    }

    @GetMapping("/registerApp")
    public RestBean getAllRegister(){
        List<RegisterApp> allRegister = usersMapper.getAllRegister();
        for (RegisterApp registerApp : allRegister) {
            String roleName = usersMapper.findRoleNameByUsers(registerApp.getRoleId());
            registerApp.setRoleName(roleName);
        }
        return RestBean.success(allRegister);
    }
    @GetMapping("/list")
    public R getList(){
        List<Users> list = userService.list();
        return new R(true,list);
    }
    
    @PostMapping("/OkRegisterApp")
    public RestBean OkRegisterApp(@RequestBody String username){
        username=username.substring(0,username.length()-1);
        System.out.println(username);
        RegisterApp registerApp = usersMapper.getRegisterApp(username);
        usersMapper.deleteRegisterApp(username);
        Users users=new Users(null,
                              registerApp.getUsername(),
                              registerApp.getPassword(),
                              registerApp.getRoleId(),
                              registerApp.getPhone(),
                              registerApp.getAddress(),
                              registerApp.getEmail()
                              );
        boolean save = userService.save(users);
        if(save)
            return RestBean.success();
        return RestBean.failure(400,null);
    }
}
